﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using CommunicationDataContract;
using System.Collections.ObjectModel;

namespace CommunicationDesktopApplication
{
    public class DesktopApplicationConnection : IDisposable
    {
        #region Private Fields
        private Guid ApplicationId = Guid.Empty;
        private Guid RemoteApplicationId = Guid.Empty;
        private NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
        private EndpointAddress endpoint;
        private IDesktopCommunicationEndPointContract CommunicationEndPoint;
        #endregion

        #region static Properties
        /// <summary>
        /// List Attached Applications
        /// </summary>
        /// <returns></returns>
        public static ObservableCollection<ApplicationInfo> AttachedApplications
        {
            get
            {
                return attachedApplications;
            }
        }
        private static ObservableCollection<ApplicationInfo> attachedApplications = new ObservableCollection<ApplicationInfo>() { new ApplicationInfo(Guid.Empty, new Byte[] { 0, 0, 0, 255 }), new ApplicationInfo(Guid.Empty, new Byte[] { 255, 0, 0, 255 }), new ApplicationInfo(Guid.Empty, new Byte[] { 0, 255, 0 }), new ApplicationInfo(Guid.Empty, new Byte[] { 0, 0, 255 }) };
        #endregion

        #region Constructors
        public DesktopApplicationConnection(DesktopServiceController desktopController, Guid remoteApplicationId)
        {
            ApplicationId = desktopController.ApplicationId;
            RemoteApplicationId = remoteApplicationId;
            endpoint = new EndpointAddress(EndpointSettings.GetDesktopApplicationEndpointAddress(RemoteApplicationId));
        }
        #endregion
        #region static Methods
        public static void UpdateAttachedApplications(DesktopServiceController desktopController, Guid[] AttachedApplicationIds)
        {
            if (AttachedApplicationIds == null)
                return;

            foreach (var AttachedApplicationId in AttachedApplicationIds)
            {
                if (AttachedApplications.FirstOrDefault(d => d.ApplicationId == AttachedApplicationId) == null)
                {
                    var appInfo = (new DesktopApplicationConnection(desktopController, AttachedApplicationId)).GetApplicationInfo();
                    if (appInfo != null)
                        AttachedApplications.Add(appInfo);
                }
            }
            var removeList = new List<ApplicationInfo>();
            foreach (var AttachedApplication in AttachedApplications)
            {
                if (!AttachedApplicationIds.Contains(AttachedApplication.ApplicationId))
                {
                    removeList.Add(AttachedApplication);
                }
            }
            foreach (var removeItem in removeList)
                AttachedApplications.Remove(removeItem);
        }
        #endregion
        #region Methods
        public ApplicationInfo GetApplicationInfo()
        {
            try
            {
                CommunicationEndPoint = ChannelFactory<IDesktopCommunicationEndPointContract>.CreateChannel(binding, endpoint);
                return CommunicationEndPoint.GetInformation(ApplicationId);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public Boolean Ping()
        {
            try
            {
                 CommunicationEndPoint = ChannelFactory<IDesktopCommunicationEndPointContract>.CreateChannel(binding, endpoint);
                 CommunicationEndPoint.Ping(ApplicationId);
                 return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        #endregion

        #region IDisposable
        public void Dispose()
        {
            CommunicationEndPoint = null;
        }
        #endregion
    }
}
